<?php
namespace Home\Logic;
class PayLogic{
	/**
	 * 公众号支付 必须以get形式传递 out_trade_no 参数
	 * 示例请看 /Application/Home/Controller/IndexController.class.php
	 * 中的weixinpay_js方法
	 */
	public function pay($order_id,$openid,$status,$money,$zhuangtai,$start_time,$end_time){
	    ini_set('date.timezone','Asia/Shanghai');
		//error_reporting(E_ERROR);
		require_once COMMON_PATH."Api/WXPAY/lib/WxPay.Api.php";
		require_once COMMON_PATH."Api/WXPAY/example/WxPay.JsApiPay.php";
		//①、获取用户openid
		$tools = new \JsApiPay();
		$attach = json_encode(['order_id'=>$order_id,'status'=>$status,'zt'=>$zhuangtai,'open_id'=>$openid]);
		//②、统一下单
		$input = new \WxPayUnifiedOrder();
		$input->SetBody("费用");
		$input->SetAttach($this->encode($attach));
		$input->SetOut_trade_no('1484865322'.date("YmdHis"));
		$input->SetTotal_fee($money*100);
		$input->SetTime_start(date("YmdHis"));
		$input->SetTime_expire(date("YmdHis", time() + 600));
		$input->SetGoods_tag("test");
		$input->SetNotify_url("http://ydp.mylei.cn/Home/PayNotify/notify");//回调地址
		$input->SetTrade_type("JSAPI");
		$input->SetOpenid($openid);
		$order = \WxPayApi::unifiedOrder($input); 
		$jsApiParameters = $tools->GetJsApiParameters($order);//返回支付数据
		$arr['canshu']=json_decode($jsApiParameters);
		$arr['order_id']=$order_id;
		$arr['start_time']=$start_time;
		$arr['end_time']=$end_time;
		$jsApiParameters=json_encode($arr);
		echo $jsApiParameters;
		exit();
		//$this->ajaxSuccess('OK',$jsApiParameters);
// 		echo $this->getbutton($jsApiParameters);
	}
	
	private function getbutton($code)
	{
		$success = U('Home/Public/success');
		$error = U('Home/Public/error');
		$button = <<<EOT
<script type="text/javascript">
function onBridgeReady(){
    var data={$code};
    WeixinJSBridge.invoke(
        'getBrandWCPayRequest', data,
        function(res){
            if(res.err_msg == "get_brand_wcpay_request:ok" ) {
    			window.location="{$success}";
                // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。
            }else{
    			window.location="{$error}";
               // alert(res.err_code+res.err_desc+res.err_msg); // 显示错误信息
            }
        }
    );
}
 if (typeof WeixinJSBridge == "undefined"){
     if( document.addEventListener ){
         document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
     }else if (document.attachEvent){
         document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
         document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
     }
 }else{
      onBridgeReady();
 }
	</script>
 </body>
</html>
EOT;
    	return $button;
    }
    private function encode($string = '', $skey = 'cxphp') {
    	$strArr = str_split ( base64_encode ( $string ) );
    	$strCount = count ( $strArr );
    	foreach ( str_split ( $skey ) as $key => $value )
    		$key < $strCount && $strArr [$key] .= $value;
    	return str_replace ( array (
    			'=',
    			'+',
    			'/'
    	), array (
    			'O0O0O',
    			'o000o',
    			'oo00o'
    	), join ( '', $strArr ) );
    }
}